home *** CD-ROM | disk | FTP | other *** search
/ Amiga Game-Power / Amiga Game-Power.iso / anwendungen / gw print / structurebrowser_v1.3 / sources / output.c < prev    next >
C/C++ Source or Header  |  1994-05-20  |  8KB  |  286 lines

  1. /*
  2.    The output routines used by the SB structure handlers. Each structure is
  3.    presented as a menu of up to 16 items; the text for these items is
  4.    contained in the static array called textlines[].
  5. */
  6.  
  7. #include "header/sb.h"
  8.  
  9. #define FLAGFIELDS 4
  10. #define MIN(x,y) ((x)<(y)?(x):(y))
  11.  
  12. extern struct IntuiText ChoiceText[], BackIText;
  13. static char textlines[MAXGADG + 1][80];
  14.  
  15. /* put
  16.  
  17.    Build a line showing the contents of one member of a system structure, and
  18.    add it to the textlines[] array, which will eventually be sent to the
  19.    screen as IntuiText.
  20.  
  21.    Parameters:
  22.       option   the number of the textlines[] element put() is to fill
  23.       stuff    a pointer to StructData structure (see header/sb.h)
  24.       base     a pointer to the system structure being examined
  25.       offset   for system structures with more than 16 members, an offset to
  26.                   the first element to be displayed on the current menu page
  27. */
  28.  
  29. put (option, stuff, base, offset)
  30. int option;
  31. struct StructData *stuff;
  32. register char *base;
  33. register int offset;
  34. {
  35. register long lnum;
  36. register int  inum;
  37. register int  i;
  38. char buf[40];
  39.  
  40.    /* move the structure member's name and type description into array */
  41.    sprintf(textlines[option], "%-16s%-24s",
  42.          stuff->membername, stuff->membertype);
  43.  
  44.    /* convert member contents to ascii according to printttype code    */
  45.    switch (stuff->printtype)
  46.    {
  47.       case PRNULL:                          /* don't print anything    */
  48.          buf[0] = '\0';
  49.          break;
  50.       case PRLONG:                          /* print a long            */
  51.          lnum = *(long *)(base + offset);
  52.          sprintf(buf, "$%8lx  %10ld", lnum, lnum);
  53.          break;
  54.       case PRINT:                           /* print an int            */
  55.          inum = *(int *)(base + offset);
  56.          sprintf(buf, "$%8x  %10d", inum, inum);
  57.          break;
  58.       case PRBYTE:                          /* print a byte            */
  59.          inum = *(base + offset);
  60.          sprintf(buf, "$%8x  %10d", inum, inum);
  61.          break;
  62.       case PRSTRING:                        /* print a string          */
  63.          if (!(lnum = *(long *)(base + offset) ))
  64.             sprintf(buf, "NULL");
  65.          else
  66.          {
  67.             /* we only have room to display 30 characters         */
  68.             for (i = 0; i < 30 && ((char *)lnum)[i]; i++)
  69.                buf[i + 1] = ((char *)lnum)[i];
  70.             buf[0] = buf[i + 1] = '\"';
  71.             buf[i + 2] = '\0';
  72.             if (((char *)lnum)[i])
  73.                strcat(buf, "...");
  74.          }
  75.          break;
  76.       case PRPTR:                           /* print a pointer         */
  77.          if (!(lnum = *(long *)(base + offset)))
  78.             sprintf(buf, "NULL");
  79.          else
  80.             sprintf(buf, "$%8lx  %10ld", lnum, lnum);
  81.          break;
  82.       case PRULONG:                         /* print an unsigned long  */
  83.          lnum = *(long *)(base + offset);
  84.          sprintf(buf, "$%8lx  %10lu", lnum, lnum);
  85.          break;
  86.       case PRUINT:                          /* print an unsigned int   */
  87.          inum = *(int *)(base + offset);
  88.          sprintf(buf, "$%8x  %10u", inum, inum);
  89.          break;
  90.       case PRUBYTE:                         /* print an unsigned byte  */
  91.          inum = *(base + offset);
  92.          sprintf(buf, "$%8x  %10u", inum, inum);
  93.          break;
  94.    }
  95.    strcat(textlines[option], buf);
  96.  
  97.    /* make corresponding IntuiText point at the newly created line */
  98.    ChoiceText[option].IText = (UBYTE *)textlines[option];
  99. }
  100.  
  101.  
  102. /* FlagPrint
  103.  
  104.    Given a title (string), an array of names (names) and a 32-bit pile of
  105.    flags (flags), build 4-up lines specifying the set flags, and install them
  106.    in the textlines[] array.
  107. */
  108.  
  109. FlagPrint (string, names, flags)
  110. char *string, **names;
  111. ULONG flags;
  112. {
  113. int i, line, fields = FLAGFIELDS;
  114. char buf[32];
  115.  
  116.    /* Use "Previous Level" as text on the escape gadget */
  117.    SetBackText(1);
  118.  
  119.    /* No indirection allowed through these lines, so start them with a '-'.
  120.       And make IntuiText point at where the strings will be.
  121.    */
  122.    for (i = 0; i < 8; i++)
  123.    {
  124.       strcpy(textlines[i], "-");
  125.       ChoiceText[i].IText = (UBYTE *)textlines[i];
  126.    }
  127.  
  128.    /* print the supplied header string */
  129.    putHeader(string, NULL);
  130.  
  131.    for (i = line = 0; i < 32; i++)
  132.    {
  133.       if ((flags & (1L << i)) && names[i])
  134.       {
  135.          sprintf(buf, "%-19s", names[i]);
  136.          strcat(textlines[line], buf);
  137.          if (!--fields)
  138.          {
  139.             ChoiceText[line].IText = (UBYTE *)textlines[line];
  140.             line++;
  141.             fields = FLAGFIELDS;
  142.          }
  143.       }
  144.    }
  145.    if (fields < FLAGFIELDS)
  146.       ChoiceText[line].IText = (UBYTE *)textlines[line];
  147. /*   while (GetChoice(line + 1))
  148.       putHeader(string, NULL)*/
  149.       
  150.    GetChoice(line + 1);
  151. }
  152.  
  153.  
  154. /* HexDump
  155.  
  156.    Dump memory as hex in byte, word or longword units. If the size argument
  157.    is -1, the dump continues until exited by the user.
  158. */
  159.  
  160. HexDump (string, address, unit, size)
  161. char *address, *string;
  162. int unit;
  163. long size;
  164. {
  165. int line = 0;
  166. int c;
  167. char *buf[80];
  168.  
  169.    BackIText.IText = (UBYTE *)" Exit Hex Dump  ";
  170.  
  171.    /* We want an infinite dump for size == -1, but settle for 0x7ffff */
  172.    if (size == -1)
  173.       size = 0x7ffff;
  174.    do
  175.    {
  176.       /* Create a header for a page of hexdump, and display it */
  177.       sprintf(buf, "%s from %lx (%ld)", string, address, address);
  178.       putHeader(buf, NULL);
  179.  
  180.       /* Dump out a page, or the specified range if less       */
  181.       if (line == MAXGADG)
  182.          line = 0;
  183.       while (line < MAXGADG && size > 0)
  184.       {
  185.          HexLine(address, unit, line++, size);
  186.          size -= 16;
  187.          address += 16;
  188.       }
  189.  
  190.       /* Let user decide to quit or do more (if there's more to do) */
  191.       c = GetChoice(size > 0 ? MAXGADG + 1 : line);
  192.    } while (size > 0 && c == MOREGADG);
  193. }
  194.  
  195.  
  196. /* HexLine
  197.  
  198.    Generate a line of hexdump in the specified unit (byte, word, long)
  199. */
  200.  
  201. HexLine (address, unit, line, size)
  202. UBYTE *address;
  203. int unit, line;
  204. long size;
  205. {
  206. USHORT i, j;
  207. char buf[80];
  208. static char hexdigit[] = "0123456789ABCDEF";
  209.  
  210.    sprintf(textlines[line], "-%6lx: ", address);
  211.  
  212.    for (i = 0; i < MIN(size, 16); i += unit)
  213.    {
  214.       switch (unit)
  215.       {
  216.          case BYTESIZE:
  217.             j = *(address + i);
  218.             sprintf(buf, "%c%c ", hexdigit[j / 16], hexdigit[j % 16]);
  219.             break;
  220.          case INTSIZE:
  221.             sprintf(buf, "%04x ", *(int *)(address + i));
  222.             break;
  223.          case PTRSIZE:
  224.             sprintf(buf, "%08lx ", *(long *)(address + i));
  225.             break;
  226.       }
  227.       strcat(textlines[line], buf);
  228.    }
  229.    ChoiceText[line].IText = (UBYTE *)textlines[line];
  230. }
  231.  
  232.  
  233. /* SetOptionText
  234.  
  235.    Fill up the textlines array by repeated calls to put().
  236. */
  237.  
  238. SetOptionText (hdrtext, data, object, size, offset)
  239. char *hdrtext;
  240. struct StructData *data;
  241. APTR object;
  242. int size, offset;
  243. {
  244. int i, sum;
  245.  
  246.    /* Set escape text to "Previous level" or "Previous page" depending on
  247.       value of offset (will be zero if this is first page of structure
  248.    */
  249.    SetBackText(offset ? 1 : 0 );
  250.  
  251.    /* Display name and address of object */
  252.    putHeader(hdrtext, object);
  253.  
  254.    /* Build lines of text while stepping through the structure */
  255.    for (i = sum = 0; i < size; i++)
  256.    {
  257.       put(i, &data[i], object, sum + offset);
  258.       sum += data[i].datasize;
  259.    }
  260.  
  261.    /* Tell caller how far we got into the structure */
  262.    return (sum + offset);
  263. }
  264.  
  265.  
  266. /* PrString
  267.  
  268.    Print a text string
  269. */
  270.  
  271. PrString (heading, string)
  272. char *heading, *string;
  273. {
  274. char *newstring, *malloc();
  275.  
  276.    putHeader(heading, NULL);
  277.    newstring = malloc(strlen(string) + 2);
  278.    *newstring = '-';
  279.    strcpy(newstring + 1, string);
  280.    ChoiceText[0].IText = (UBYTE *)newstring;
  281.    GetChoice(1);
  282.    free(newstring);
  283. }
  284.  
  285.  
  286.